#!/bin/bash

readonly  BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $BASE_DIR || exit;

shopt -s expand_aliases
[ "$(uname)" == "Darwin" ] &&  alias date='gdate'


pwd



FILE_DIR="/home/sky/Desktop/bbw/bbw-data-files"
MERGE_FILE_DIR="/home/sky/Desktop/bbw"

start='2024-08-01'
end='2025-07-01'
year_month=$(date -d  "${start}" +%Y%m)   #  yyyy-MM-dd  ==>  yyyymm

merge_file=$MERGE_FILE_DIR"/tlbh_txn_${year_month}.csv"
touch   $merge_file

while  [[ $start < $end   ]]   ; do

    day=$(date -d "$start" +%d)
    txn_data_file=$FILE_DIR"/${year_month}${day}/tlbh_txn_${year_month}${day}.csv"
    echo "[$(date)] executing  ${txn_data_file}"
    sed -n '1,$p'     $txn_data_file  >>   "${merge_file}"

    start=$(date -d "$start + 1 day" +%F)
    if [[ $year_month !=   $(date -d  "${start}" +%Y%m)  ]]; then
        year_month=$(date -d  "${start}" +%Y%m)
        merge_file=$MERGE_FILE_DIR"/tlbh_txn_${year_month}.csv"
        touch   ${merge_file}
        echo   "creating  new  merge file ${merge_file} "
    fi


done



